<?php
namespace Purse\Controller;
use Purse\Controller\PurseController;

/**
 * 红包模块
 */
class RedPacketsController extends PurseController {
	protected $_redpackets;
	protected $_redpackets_detail;
	
	protected function _initialize(){
		parent::_initialize();
		
		$this->_moduleid = 112;
		$this->_chat = new \Common\Model\ChatModel();
		$this->_chat_cust = new \Common\Model\ChatCustModel();
		$this->_redpackets = new \Purse\Model\RedPacketsModel();
		$this->_redpackets_detail = new \Purse\Model\RedPacketsDetailModel();
	}
    
	
	
	static private function countWeight($data){
		$weight=0;
		$temp=array();
		foreach($data as &$v){
			$weight+=$v['weight'];
			for($i=0;$i<$v['weight'];$i++){
				$temp[]=$v;//放大数组
			}
		}
		$int=mt_rand(0,$weight-1);//获取一个随机数
		$result=$temp[$int];
		return $result;
	}
	
    
	/**
	 * 创建红包
	 */
	public function createPacketsAction(){
		$this->_auth();
		$amount = I('request.amount');
		$number = (int)I('request.number');
		$blessing = I('request.blessing');
		$type = (int)I('request.type');//1为随机包，2为定额包
		$paypassword = I('request.paypassword');
		$this->_createPackets($amount , $number , $blessing , $type , $paypassword);
	}
	
	private function _createPackets($amount , $number , $blessing , $type , $paypassword){
		$amount = number_format($amount, 2, '.', '');
		if((bccomp($amount, '1.00', 2) < 0 && $type == 1) || empty($number) || $number > 100 || !in_array($type , array(1,2))){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
		}
		
		
		$totalAmount = ($type == "1") ? $amount : $amount * $number;
		
		$open_id = $this->_custInfo['ucropenid'];
		
		$blessingTmp = empty($blessing)?'发红包':$blessing;
		//支付
		//预付
		$ret = \ucr::makeImprest($open_id,'',1,$totalAmount*100,'发红包',$blessingTmp);
		if(empty($ret['imprest_code'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$code = $ret['imprest_code'];
		
		//取支付token
		$ret = \ucr::getPayToken($open_id,$paypassword);
		if(empty($ret['pay_token'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		$pay_token = $ret['pay_token'];

        $notify_url = C('PAY_NOTIFY');
		//支付
		$ret = \ucr::payImprestV2($this->_custInfo['custid'],$open_id,$code,$notify_url,$pay_token);
		if(!empty($ret['err_code'])){
			$this->status = $ret['err_code'];
			$this->msg = $ret['err_msg'];
			$this->outPut();
		}
		//$subtradeno = $ret['sub_trade_no'];
        $subtradeno = '';
		$leftamount = 0.00;
		
		//记录转出方流水
		$ret = $this->_amountTakeNotes($this->_custInfo['custid'] , 0 , -$totalAmount , $leftamount , "发红包" , 0  , $blessingTmp , $code , $subtradeno);
		if(!$ret){
			$this->status = 2505;
			$this->msg = L('STATUS_2505');
			$this->outPut();
		}
		
		
		
		
		$data = array();
		$data['amount'] = $amount;
		$data['number'] = $number;
		$data['custid'] = $this->_custInfo['custid'];
		$data['type'] = $type;
		$data['blessing'] = $blessing;
		$data['code'] = $code;
		$packetsid = $this->_redpackets->addPackets($data);
		if($packetsid){
			$packetskey = \cachekey::$key['BASE_KEY_CONF']['RED_PACKETS'] . $packetsid;
			if($type == 1){
				$total = $amount * 100;
				$average = ceil($total / $number);
				$averageMin = $average * 0.07;//ceil($average - $average * 0.3);
				$averageMax = $average * 1.95;//ceil($average + $average * 0.2);
				$weightData = array(
						//0=>array('min'=>1,'max'=>$total * 0.6,'weight'=>4),
						//1=>array('min'=>$averageMin,'max'=>$averageMax,'weight'=>96)
						//0=>array('min'=>$average * 0.07,'max'=>$average * 1.95,'weight'=>100),
						0=>array('min'=>$averageMin,'max'=>$averageMax,'weight'=>100),
				);
				
				
				
				$tempPacket = array();
				$this->_cacle($total , $number , $weightData , $tempPacket);
				
				$tmpAmount = 0.00;
				$tmpLuckIdx = 0;
				for($j=0;$j<count($tempPacket);$j++){
					if($tempPacket[$j]['amount'] > $tmpAmount){
						$tmpAmount = $tempPacket[$j]['amount'];
						$tmpLuckIdx = $j;
					}
				}
				$tempPacket[$tmpLuckIdx]['isluck'] = "1";
				
				for($j=0;$j<count($tempPacket);$j++){
					$this->_mycache->sadd($packetskey , json_encode($tempPacket[$j]));
				}
				
			}else{
				for($i=1;$i<=$number;$i++){
					$this->_mycache->sadd($packetskey , json_encode(array('id'=>$i,'amount'=>$amount,'isluck'=>"0")));
				}
			}
			
			$this->data = array('packetsid'=>$packetsid);
			$this->outPut();
		}else{
			$this->status = 2500;
			$this->msg = L('STATUS_2500');
			$this->outPut();
		}
	}
	
	
	
	private static function _cacle($total , $number , $weightData , &$tempPacket){
		$tempPacket = array();
		$ret = self::countWeight($weightData);
		$leftamount = $total;
		for($i=1;$i<=$number;$i++){
			if($i == $number){
				$tempPacket[] = array('id'=>$i,'amount'=>$leftamount / 100,'isluck'=>'0');
				if($leftamount > $ret['max']) self::_cacle($total , $number , $weightData , $tempPacket);
				break;
			}
				
			$min = $ret['min'];
			$max = $leftamount - ($number - $i) * $ret['min'];
			if($max > $ret['max']) $max = $ret['max'];
				
			if($min > $max) $min = 1;
				
			$amountunit = mt_rand($min , $max);
			$tempPacket[] = array('id'=>$i,'amount'=>$amountunit / 100,'isluck'=>'0');
			$leftamount = $leftamount - $amountunit;
		}
	}
	
	
	
	/**
	 * 发红包
	 */
	public function sendPacketsAction(){
		$this->_auth();
		$packetsid = (int)I('request.packetsid');
		$tagetid = (int)I('request.tagetid');  //用户id或群id
		$tagettype = (int)I('request.tagettype');//1为私包 2为群包
		$this->_sendPackets($packetsid , $tagetid , $tagettype);
	}
	
	private function _sendPackets($packetsid , $tagetid , $tagettype){
		if(empty($packetsid) || empty($tagetid) || ($tagettype != 1 && $tagettype != 2)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		if($tagettype == 1){
			$custInfo = $this->_getCustInfo($tagetid);
			if(empty($custInfo)){
				$this->status = 1001;
				$this->msg = L('STATUS_1001');
				$this->outPut();
			}
			$tcustid = $tagetid;
			$chatid = 0;
		}else{
			$this->_checkCurrentCustInChat($tagetid);
			$tcustid = 0;
			$chatid = $tagetid;
		}
		
		$packetsInfo = $this->_redpackets->getNewPacketsById($packetsid);
		if(empty($packetsInfo)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data = array();
		$data['packetsid'] = $packetsid;
		$data['tcustid'] = $tcustid;
		$data['chatid'] = $chatid;
		$data['sendtime'] = date("Y-m-d H:i:s");
		$ret = $this->_redpackets->updatePackets($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 2506;
			$this->msg = L('STATUS_2506');
			$this->outPut();
		}
	}
    
	
	/**
	 * 捡红包
	 */
	public function pickPacketsAction(){
		$this->_auth();
		$packetsid = (int)I('request.packetsid');
		$this->_pickPackets($packetsid);
	}
	
	private function _pickPackets($packetsid){
		if(empty($packetsid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$packetsInfo = $this->_redpackets->getPacketsById($packetsid);
		if(empty($packetsInfo)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		if(!empty($packetsInfo['tcustid']) && $packetsInfo['tcustid'] != $this->_custInfo['custid'] && $packetsInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;//不能领取发给别人的私包,防止非法截获
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		if(!empty($packetsInfo['chatid'])) $this->_checkCurrentCustInChat($packetsInfo['chatid']);
		
		$packetsAllInfo = $this->_getPacketsAllInfo($packetsInfo);
		
		/* $expired = (time()-strtotime($packetsInfo['createtime']) > (int)C('RED_PACKETS_EXPIRE')) ? "1" : "0";
		if(!empty($expired) && $packetsAllInfo['isOpened'] == "0"){
			$this->status = 2503;
			$this->msg = L('STATUS_2503');
			$this->outPut();
		}
		
		if($packetsInfo['number'] == $packetsInfo['opennumber'] && $packetsAllInfo['isOpened'] == "0"){
			$this->status = 2507;
			$this->msg = L('STATUS_2507');
			$this->outPut();
		} */
		
		$this->data = $packetsAllInfo;
		$this->outPut();
	}
	
	
	private function _getPacketsAllInfo($packetsInfo){
		$data = array();
		$data['packetsid'] = $packetsInfo['packetsid'];
		$packetsOpenList = $this->_redpackets_detail->getPacketsList($data);
		$packetsOpenList = $packetsOpenList['data'];
		
		$resultData = array();
		$isOpened = "0";
		$pickAmount = "0.00";
		if(!empty($packetsOpenList)){
			foreach($packetsOpenList as $idx=>$packets){
				$custInfo = $this->_getCustInfo($packets['custid']);
		
				$resultData[$idx]['custid'] = $packets['custid'];
				$resultData[$idx]['nickname'] = $custInfo['nickname'];
				$resultData[$idx]['amount'] = (string)$packets['amount'];
				$resultData[$idx]['time'] = $packets['createtime'];
				
				if($packetsInfo['number'] == $packetsInfo['opennumber'])
					$resultData[$idx]['isluck'] = empty($packetsInfo['chatid']) ? "0" : $packets['isluck'];
				else 
					$resultData[$idx]['isluck'] = "0";
				
				$resultData[$idx]['thankswords'] = $packets['thankswords'];
		
				if($packets['custid'] == $this->_custInfo['custid']){
					$isOpened = "1";
					$pickAmount = (string)$packets['amount'];
				}
			}
		}
		
		if(empty($resultData) && !empty($packetsInfo['tcustid'])){//私包未打开的情况
			$custInfo = $this->_getCustInfo($packetsInfo['tcustid']);
			
			$resultData[0]['custid'] = $custInfo['custid'];
			$resultData[0]['nickname'] = $custInfo['nickname'];
			$resultData[0]['amount'] = (string)$packetsInfo['amount'];
			$resultData[0]['time'] = '';
			$resultData[0]['isluck'] = '0';
			$resultData[0]['thankswords'] = '';
		}
		
		
		$custInfo = $this->_getCustInfo($packetsInfo['custid']);
		
		$redPacketsInfo['packetsid'] = $packetsInfo['packetsid'];
		$redPacketsInfo['createtime'] = $packetsInfo['createtime'];
		$redPacketsInfo['type'] = $packetsInfo['type'];
		$redPacketsInfo['custid'] = $packetsInfo['custid'];
		$redPacketsInfo['tcustid'] = $packetsInfo['tcustid'];
		$redPacketsInfo['chatid'] = $packetsInfo['chatid'];
		$redPacketsInfo['nickname'] = $custInfo['nickname'];
		$redPacketsInfo['amount'] = ($packetsInfo['type']=="1") ? (string)$packetsInfo['amount'] : (string)($packetsInfo['amount'] * $packetsInfo['number']);
		$redPacketsInfo['openamount'] = $packetsInfo['openamount'];
		$redPacketsInfo['blessing'] = $packetsInfo['blessing'];
		$redPacketsInfo['number'] = $packetsInfo['number'];
		$redPacketsInfo['opennumber'] = $packetsInfo['opennumber'];
		$redPacketsInfo['leftnum'] = $redPacketsInfo['number'] - $redPacketsInfo['opennumber'];
		$redPacketsInfo['leftnum'] = (string)$redPacketsInfo['leftnum'];
		$redPacketsInfo['expired'] = (time()-strtotime($packetsInfo['createtime']) > (int)C('RED_PACKETS_EXPIRE')) ? "1" : "0";
		
		$redPacketsInfo['spendtime'] = strtotime($packetsInfo['updatetime']) - strtotime($packetsInfo['sendtime']) + 1;
		
		return array('redPacketsInfo'=>$redPacketsInfo,'packetsOpenList'=>$resultData,'isOpened'=>$isOpened,'pickAmount'=>$pickAmount);
	}
	
	
	
	/**
	 * 拆红包
	 */
	public function openPacketsAction(){
		$this->_auth();
		$packetsid = (int)I('request.packetsid');
		$this->_openPackets($packetsid);
	}
	
	private function _openPackets($packetsid){
		if(empty($packetsid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$packetsInfo = $this->_redpackets->getPacketsById($packetsid);
		if(empty($packetsInfo) || (empty($packetsInfo['chatid']) && empty($packetsInfo['tcustid']))){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$expired = (time()-strtotime($packetsInfo['createtime']) > (int)C('RED_PACKETS_EXPIRE')) ? "1" : "0";//(int)C('RED_PACKETS_EXPIRE')
		if(!empty($expired)){
			$this->status = 2503;
			$this->msg = L('STATUS_2503');
			$this->outPut();
		}
		
		if($packetsInfo['number'] == $packetsInfo['opennumber']){
			$this->status = 2507;
			$this->msg = L('STATUS_2507');
			$this->outPut();
		}
		
		if(!empty($packetsInfo['tcustid']) && $packetsInfo['tcustid'] != $this->_custInfo['custid'] && $packetsInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;//不能领取发给别人的私包,防止非法截获
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		if(!empty($packetsInfo['tcustid']) && $packetsInfo['number'] == "1" && $packetsInfo['custid'] == $this->_custInfo['custid']){
			$this->status = 1001;//只有一个包的时候，发包人自己不能领取发给别人的私包
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		
		$chatid = $packetsInfo['chatid'];
		if(!empty($chatid)) $this->_checkCurrentCustInChat($chatid);
		
		$packetsOpenedkey = \cachekey::$key['BASE_KEY_CONF']['RED_PACKETS_OPENED'] . $packetsInfo['packetsid'];
		$packetsOpenList = $this->_mycache->smembers($packetsOpenedkey);//已打开的红包
		$isOpened = "0";
		if(!empty($packetsOpenList)){
			foreach($packetsOpenList as $idx=>$packets){
				$packetsArr = json_decode($packets , true);
				if($packetsArr['custid'] == $this->_custInfo['custid']) $isOpened = "1";
			}
		}else{
			$data = array();
			$data['custid'] = $this->_custInfo['custid'];
			$data['packetsid'] = $packetsInfo['packetsid'];
			$myCurPacketsList = $this->_redpackets_detail->getPacketsList($data);
			if($myCurPacketsList['total'] > 0) $isOpened = "1";
		}
		
		if(!empty($isOpened)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$packetskey = \cachekey::$key['BASE_KEY_CONF']['RED_PACKETS'] . $packetsid;
		$ret = $this->_mycache->spop($packetskey);
		if(empty($ret)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$ret = json_decode($ret , true);
		$amount = number_format($ret['amount'], 2, '.', '');
		$isluck = $ret['isluck'];
		
		if(bccomp($amount, '0.01', 2) >= 0){
			$code = $packetsInfo['code'];
			$open_id = $this->_custInfo['ucropenid'];
			
			//领取
			$ret = \ucr::getPreAmountV2($open_id,$code,$amount*100,'领取红包','领取红包');
			if(!empty($ret['err_code'])){
				$this->status = $ret['err_code'];
				$this->msg = $ret['err_msg'];
				$this->outPut();
			}
			$subtradeno = $ret['sub_trade_no'];
			$leftamount = 0.00;
			
			//记录转入方流水
			$ret1 = $this->_amountTakeNotes($this->_custInfo['custid'] , $packetsInfo['custid'] , $amount , $leftamount , "领取红包" , 0 , $packetsInfo['blessing'] , $code , $subtradeno);
			$data = array();
			$data['packetsid'] = (int)$packetsid;
			$data['custid'] = $this->_custInfo['custid'];
			$data['chatid'] = (int)$chatid;
			$data['fcustid'] = (int)$packetsInfo['custid'];
			$data['amount'] = $amount;
			$data['isluck'] = (int)$isluck;
			$ret2 = $this->_redpackets_detail->addPacketsDetail($data);
			if($ret1 && $ret2){
				$packetsOpenedkey = \cachekey::$key['BASE_KEY_CONF']['RED_PACKETS_OPENED'] . $packetsid;
				$packetsOpenedInfo['custid'] = $this->_custInfo['custid'];
				$packetsOpenedInfo['amount'] = $amount;
				$packetsOpenedInfo['time'] = date("Y-m-d H:i:s");
				$packetsOpenedInfo['isluck'] = $isluck;
				$this->_mycache->sadd($packetsOpenedkey , json_encode($packetsOpenedInfo)); 
				
				$this->_redpackets->opennumberInc($packetsid , $amount);
				
				$packetsInfo = $this->_redpackets->getPacketsById($packetsid);
				$packetsAllInfo = $this->_getPacketsAllInfo($packetsInfo);
				
				if($packetsInfo['custid'] != $this->_custInfo['custid']){
					//$content = L('PACKETS_SENDER_MSG' , array('nickname' => $this->_custInfo['nickname']));//"xxx领取了你发的红包";
					$content = "{0}领取了你发的红包";
					$content_param[0] = array('custid'=>$this->_custInfo['custid'],'nickname'=>$this->_custInfo['nickname']);
					
					$this->_sendPacketsMsg($this->_custInfo['nickname'] , $this->_custInfo['custid'] , $packetsInfo['custid'] ,$this->_custInfo['custid'] ,  $content , $content_param , $chatid);
					
					//$content = L('PACKETS_GETTER_MSG' , array('nickname' => $packetsAllInfo['redPacketsInfo']['nickname']));//"你领取了xxx发的红包";
					$content = "你领取了{0}发的红包";
					$content_param[0] = array('custid'=>$packetsAllInfo['redPacketsInfo']['custid'],'nickname'=>$packetsAllInfo['redPacketsInfo']['nickname']);
					$this->_sendPacketsMsg($packetsAllInfo['redPacketsInfo']['nickname'] , $packetsInfo['custid'] , $this->_custInfo['custid'] ,$packetsInfo['custid'], $content , $content_param , $chatid);
				}else{
					$content = L('PACKETS_SELFGET_MSG');//"你领取了自己发的红包";
					$content_param = [];
					$this->_sendPacketsMsg($packetsAllInfo['redPacketsInfo']['nickname'] , $packetsInfo['custid'] , $packetsInfo['custid'] , $packetsInfo['custid'] , $content , $content_param , $chatid);
				}
				
				$this->data = $packetsAllInfo;
				$this->outPut();
			}else{
				$this->status = 2501;
				$this->msg = L('STATUS_2501');
				$this->outPut();
			}
		}else{
			$this->status = 2502;
			$this->msg = L('STATUS_2502');
			$this->outPut();
		}
	}
	
	
	private function _sendPacketsMsg($fnickname , $fcustid , $tcustid , $session_id , $content , $content_param , $chatid=0){
		$msg['from_cust_name'] = $fnickname;
		$msg['from_cust_id'] = (int)$fcustid;
		$msg['type'] = 33;
		$msg['session_type'] = $chatid > 0 ? 2 : 1;
		$msg['session_id'] = $chatid > 0 ? (int)$chatid : (int)$session_id;
		$msg['content'] = $content;
		$msg['content_param'] = $content_param;
		
		\im::sendMsg(3, array($tcustid), array(), $msg , 1);
	}
	
	
	/**
	 * 红包领取记录
	 */
	public function myPacketsListAction(){
		$this->_auth();
		$year = (int)I('request.year');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_myPacketsList($year , $pageIndex , $pageSize);
	}
	
	
	private function _myPacketsList($year , $pageIndex , $pageSize){
		if(empty($year) || empty($pageIndex) || empty($pageSize)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $this->_custInfo['custid'];
		$myPacketsList = $this->_redpackets_detail->getPacketsList($data);
		if(!empty($myPacketsList['data'])){
			foreach($myPacketsList['data'] as &$myPackets){
				$custInfo = $this->_getCustInfo($myPackets['fcustid']);
				$myPackets['fnickname'] = $custInfo['nickname'];
			}
		}
		
		$myPacketsTotalAmount = "0.00";
		if(!empty($myPacketsList['totalamount'])){
			foreach($myPacketsList['totalamount'] as &$totalamount){
				if($totalamount['custid'] == $this->_custInfo['custid']){
					$myPacketsTotalAmount = $totalamount['totalamount'];
				}
			}
		}
		
		$this->data = array('myPacketsTotalAmount'=>$myPacketsTotalAmount,'myPacketsNum'=>$myPacketsList['total'],'myPacketsLuckNum'=>$myPacketsList['lucktotal'],'myPacketsList'=>$myPacketsList['data']);
		$this->outPut();
	}
	
	
	/**
	 * 红包发送记录
	 */
	public function mySendPacketsListAction(){
		$this->_auth();
		$year = (int)I('request.year');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_mySendPacketsList($year , $pageIndex , $pageSize);
	}
	
	
	private function _mySendPacketsList($year , $pageIndex , $pageSize){
		if(empty($year) || empty($pageIndex) || empty($pageSize)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $this->_custInfo['custid'];
		$myPacketsList = $this->_redpackets->getPacketsList($data);
		if(!empty($myPacketsList['data'])){
			foreach($myPacketsList['data'] as &$myPackets){
				if($myPackets['type']=="2"){
					$myPackets['amount'] = $myPackets['amount'] * $myPackets['number'];
					$myPackets['amount'] = (string)$myPackets['amount'];
				}
			}
		}
	
		$myPacketsTotalAmount = "0.00";
		if(!empty($myPacketsList['totalamount'])){
			foreach($myPacketsList['totalamount'] as &$totalamount){
				if($totalamount['custid'] == $this->_custInfo['custid']){
					$myPacketsTotalAmount = $totalamount['totalamount'];
				}
			}
		}
	
		$this->data = array('myPacketsTotalAmount'=>$myPacketsTotalAmount,'myPacketsNum'=>$myPacketsList['total'],'myPacketsList'=>$myPacketsList['data']);
		$this->outPut();
	}
	
	
	/**
	 * 红包领取年份
	 */
	public function packetsOpenYearAction(){
		$startYear = 2016;
		$currYear = date("Y");
		
		for($i=$startYear;$i<=$currYear;$i++){
			$yearList[] = (string)$i;
		}
		
		$this->data = $yearList;
		$this->outPut();
	}
	
	
	
	private function _checkCurrentCustInChat($chatid){
		if(empty($chatid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$listKey = \cachekey::$key['BASE_KEY_CONF']['CHAT_CUST'].$chatid;
		$custList = $this->_mycache->get($listKey);//取聊天群成员缓存信息
		if(!empty($custList)){
			foreach($custList as $k=>&$v){
				if($v['custid'] == $this->_custInfo['custid']) return true;
			}
			
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		 
		$chatInfo = $this->_chat->getChatInfoById($chatid);
		if(empty($chatInfo)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}else{
			$result = $this->_chat_cust->getCust($chatid,$this->_custInfo['custid']);
			if(empty($result)){
				$this->status = 1001;
				$this->msg = L('STATUS_1001');
				$this->outPut();
			}
		}
	}
	
	//答谢
	public function returnThanksAction(){
		$this->_auth();
		$packetsid = (int)I('request.packetsid');
		$thankswords = I('request.thankswords');
		$this->_returnThanks($packetsid , $thankswords);
	}
	
	private function _returnThanks($packetsid , $thankswords){
		if(empty($packetsid) || empty($thankswords)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$data['packetsid'] = $packetsid;
		$data['custid'] = $this->_custInfo['custid'];
		$data['thankswords'] = $thankswords;
		
		$this->_redpackets_detail->updatePacketsDetail($data);
		
		$this->outPut();
	}
	
	
	
	//回收过期红包
	public function returnRedPachetsAction(){
		$packetsList = $this->_redpackets->getExpireList();
		//print_r($packetsList);
		if(!empty($packetsList)){
			foreach($packetsList as $packets){
				$packetsid = $packets['packetsid'];
				$number = $packets['number'];
				$type = (int)$packets['type'];
				$amount = $packets['amount'];
				$openamount = $packets['openamount'];
				
				if($type == 1){
					$amount = $amount - $openamount;
				}else{
					$amount = $amount * $number - $openamount;
				}
					
				if(bccomp($amount, '0.01', 2) >= 0){
					
					//绑定用户
					$ret = \ucr::bindUser($packets['custid']);
					if(empty($ret['open_id'])){
						continue;
					}
					
					//领取
					$ret = \ucr::getPreAmountV2($ret['open_id'],$packets['code'],$amount*100,"回收红包","回收红包");
					if(!empty($ret['err_code'])){
						continue;
					}
					$subtradeno = $ret['sub_trade_no'];
					$leftamount = 0.00;

					//记录流水
					$ret = $this->_amountTakeNotes($packets['custid'] , 0 , $amount , $leftamount , "回收红包" , 0 , '' , $packets['code'] , $subtradeno);
					if($ret){
						$data['packetsid'] = $packetsid;
						$data['isreturn'] = 1;
						$this->_redpackets->updatePackets($data);
					}
				}else{
					$data['packetsid'] = $packetsid;
					$data['isreturn'] = 1;
					$this->_redpackets->updatePackets($data);
				}
			}
		}
	}
}
